{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from table_evaluator import *\n",
    "import numpy as np\n",
    "from pathlib import Path\n",
    "import dython\n",
    "from dython.nominal import associations, numerical_encoding, compute_associations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert dython.__version__ == '0.5.1', 'Dython version should be version 0.5.1'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set nominal columns and load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "nominal_cols = ['trans_type', 'trans_operation', 'trans_k_symbol']\n",
    "data_folder = Path('../data')\n",
    "test_data_folder = Path('../data/tests')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "real = pd.read_csv(data_folder/'real_test_sample.csv')\n",
    "fake = pd.read_csv(data_folder/'fake_test_sample.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create numerical encodings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "real_num_enc = numerical_encoding(real, nominal_columns=nominal_cols)\n",
    "fake_num_enc = numerical_encoding(fake, nominal_columns=nominal_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "real_num_enc.to_csv(test_data_folder/'real_test_sample_numerical_encoded.csv', index=False)\n",
    "fake_num_enc.to_csv(test_data_folder/'fake_test_sample_numerical_encoded.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create Associations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "real_assoc = compute_associations(real, nominal_columns=nominal_cols)\n",
    "real_assoc_theil = compute_associations(real, nominal_columns=nominal_cols, theil_u=True)\n",
    "\n",
    "fake_assoc = compute_associations(fake, nominal_columns=nominal_cols)\n",
    "fake_assoc_theil = compute_associations(fake, nominal_columns=nominal_cols, theil_u=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Give the index a name, so we can set this column as index on loading the csv\n",
    "# for i in [real_assoc, real_assoc_theil, fake_assoc, fake_assoc_theil]:\n",
    "#     i.index.rename('index', inplace=True)\n",
    "\n",
    "real_assoc.to_csv(test_data_folder/'real_associations.csv')\n",
    "real_assoc_theil.to_csv(test_data_folder/'real_associations_theil.csv')\n",
    "\n",
    "fake_assoc.to_csv(test_data_folder/'fake_associations.csv')\n",
    "fake_assoc_theil.to_csv(test_data_folder/'fake_associations_theil.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trans_id</th>\n",
       "      <th>account_id</th>\n",
       "      <th>trans_amount</th>\n",
       "      <th>balance_after_trans</th>\n",
       "      <th>trans_type</th>\n",
       "      <th>trans_operation</th>\n",
       "      <th>trans_k_symbol</th>\n",
       "      <th>trans_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>trans_id</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.522830</td>\n",
       "      <td>-0.220340</td>\n",
       "      <td>0.028876</td>\n",
       "      <td>0.474287</td>\n",
       "      <td>0.835292</td>\n",
       "      <td>0.841451</td>\n",
       "      <td>0.054574</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>account_id</th>\n",
       "      <td>0.522830</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.025829</td>\n",
       "      <td>0.108967</td>\n",
       "      <td>0.048279</td>\n",
       "      <td>0.088169</td>\n",
       "      <td>0.164624</td>\n",
       "      <td>0.052132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trans_amount</th>\n",
       "      <td>-0.220340</td>\n",
       "      <td>0.025829</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.394746</td>\n",
       "      <td>0.193900</td>\n",
       "      <td>0.531158</td>\n",
       "      <td>0.494940</td>\n",
       "      <td>0.016639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>balance_after_trans</th>\n",
       "      <td>0.028876</td>\n",
       "      <td>0.108967</td>\n",
       "      <td>0.394746</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.124192</td>\n",
       "      <td>0.230062</td>\n",
       "      <td>0.178185</td>\n",
       "      <td>0.123935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trans_type</th>\n",
       "      <td>0.474287</td>\n",
       "      <td>0.048279</td>\n",
       "      <td>0.193900</td>\n",
       "      <td>0.124192</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.459150</td>\n",
       "      <td>0.250205</td>\n",
       "      <td>0.031496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trans_operation</th>\n",
       "      <td>0.835292</td>\n",
       "      <td>0.088169</td>\n",
       "      <td>0.531158</td>\n",
       "      <td>0.230062</td>\n",
       "      <td>0.904474</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.617749</td>\n",
       "      <td>0.093090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trans_k_symbol</th>\n",
       "      <td>0.841451</td>\n",
       "      <td>0.164624</td>\n",
       "      <td>0.494940</td>\n",
       "      <td>0.178185</td>\n",
       "      <td>0.482048</td>\n",
       "      <td>0.604179</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.107945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trans_date</th>\n",
       "      <td>0.054574</td>\n",
       "      <td>0.052132</td>\n",
       "      <td>0.016639</td>\n",
       "      <td>0.123935</td>\n",
       "      <td>0.031496</td>\n",
       "      <td>0.093090</td>\n",
       "      <td>0.107945</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     trans_id  account_id  trans_amount  balance_after_trans  \\\n",
       "trans_id             1.000000    0.522830     -0.220340             0.028876   \n",
       "account_id           0.522830    1.000000      0.025829             0.108967   \n",
       "trans_amount        -0.220340    0.025829      1.000000             0.394746   \n",
       "balance_after_trans  0.028876    0.108967      0.394746             1.000000   \n",
       "trans_type           0.474287    0.048279      0.193900             0.124192   \n",
       "trans_operation      0.835292    0.088169      0.531158             0.230062   \n",
       "trans_k_symbol       0.841451    0.164624      0.494940             0.178185   \n",
       "trans_date           0.054574    0.052132      0.016639             0.123935   \n",
       "\n",
       "                     trans_type  trans_operation  trans_k_symbol  trans_date  \n",
       "trans_id               0.474287         0.835292        0.841451    0.054574  \n",
       "account_id             0.048279         0.088169        0.164624    0.052132  \n",
       "trans_amount           0.193900         0.531158        0.494940    0.016639  \n",
       "balance_after_trans    0.124192         0.230062        0.178185    0.123935  \n",
       "trans_type             1.000000         0.459150        0.250205    0.031496  \n",
       "trans_operation        0.904474         1.000000        0.617749    0.093090  \n",
       "trans_k_symbol         0.482048         0.604179        1.000000    0.107945  \n",
       "trans_date             0.031496         0.093090        0.107945    1.000000  "
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(test_data_folder/'real_associations_theil.csv', index_col='Unnamed: 0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
